<template>
  <div class="report-container">
    <el-card shadow="never">
      <!-- 搜索栏 -->
      <div class="filter-header">
        <el-input
          v-model="filter.id"
          placeholder="编号"
          style="width: 120px"
          class="filter-item"
          clearable
          @keyup.enter.native="handleSearch"
        />
        <el-input
          v-model="filter.category"
          placeholder="类别"
          style="width: 120px; margin-left: 10px"
          class="filter-item"
          clearable
          @keyup.enter.native="handleSearch"
        />
        <el-button 
          type="primary" 
          style="margin-left: 10px"
          @click="handleSearch"
        >
          <i class="el-icon-search"></i> 搜索
        </el-button>
        <el-button 
          style="margin-left: 10px"
          @click="resetSearch"
        >
          <i class="el-icon-refresh"></i> 重置
        </el-button>
      </div>

      <!-- 数据表格 -->
      <el-table
        :data="filteredData"
        style="width: 100%"
        header-row-class-name="table-header"
        stripe
      >
        <el-table-column prop="id" label="编号" width="80" align="center"/>
        <el-table-column prop="category" label="类别" width="180"/>
        <el-table-column prop="requirement" label="填写要求" width="120"/>
        <el-table-column prop="deadline" label="截至时间" width="120" align="center"/>
        <el-table-column prop="content" label="周报内容" width="120">
          <template slot-scope="scope">
            {{ scope.row.content || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="score" label="分数" width="100" align="center"/>
        <el-table-column prop="actualScore" label="实际得分" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.actualScore || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="comment" label="教师评语" width="120">
          <template slot-scope="scope">
            {{ scope.row.comment || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <div style="display: inline-flex;">
              <el-button 
                type="primary" 
                size="small"
                @click="handleEdit(scope.row)"
                style="margin-right: 8px;"
              >
                <i class="el-icon-edit"></i> 修改
              </el-button>
              <el-button 
                type="danger" 
                size="small"
                @click="handleDelete(scope.row)"
              >
                <i class="el-icon-delete"></i> 删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页信息 -->
      <div class="pagination-wrapper">
        <span class="pagination-text">第1到{{ filteredData.length }}条，共{{ filteredData.length }}条记录。</span>
        <div class="page-size">
          <span>10条记录每页</span>
          <i class="el-icon-arrow-down"></i>
        </div>
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog 
      :title="'编辑周报 - ' + form.category"
      :visible.sync="editDialogVisible"
      width="50%"
      @closed="handleDialogClosed"
    >
      <el-form 
        :model="form" 
        :rules="rules" 
        ref="form" 
        label-width="120px"
      >
        <el-form-item label="编号" prop="id">
          <el-input v-model="form.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="category">
          <el-input v-model="form.category" placeholder="请输入类别"></el-input>
        </el-form-item>
        <el-form-item label="填写要求" prop="requirement">
          <el-input 
            type="textarea"
            :rows="3"
            v-model="form.requirement" 
            placeholder="请输入填写要求"
          ></el-input>
        </el-form-item>
        <el-form-item label="截至时间" prop="deadline">
          <el-date-picker
            v-model="form.deadline"
            type="datetime"
            placeholder="选择截至时间"
            value-format="yyyy-MM-dd"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="周报内容" prop="content">
          <el-input 
            type="textarea"
            :rows="4"
            v-model="form.content" 
            placeholder="请输入周报内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="分数" prop="score">
          <el-input v-model="form.score" placeholder="请输入分数"></el-input>
        </el-form-item>
        <el-form-item label="实际得分" prop="actualScore">
          <el-input v-model="form.actualScore" placeholder="请输入实际得分"></el-input>
        </el-form-item>
        <el-form-item label="教师评语" prop="comment">
          <el-input 
            type="textarea"
            :rows="3"
            v-model="form.comment" 
            placeholder="请输入教师评语"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      filter: {
        id: '',
        category: '',
        search: ''
      },
      tableData: [
        {id:11, category:'第1次周报', requirement:'111111111', deadline:'2020-07-24', content:'-', score:'25.00', actualScore:'-', comment:'-', action:21},
        {id:11, category:'第2次周报', requirement:'111111111', deadline:'2020-08-02', content:'-', score:'25.00', actualScore:'-', comment:'-', action:21},
        {id:11, category:'第3次周报', requirement:'111111111', deadline:'2020-08-11', content:'-', score:'25.00', actualScore:'-', comment:'-', action:21},
        {id:11, category:'第4次周报', requirement:'111111111', deadline:'2020-08-20', content:'-', score:'25.00', actualScore:'-', comment:'-', action:21},
        {id:11, category:'pyfron程序设计基础总结', requirement:'111111111', deadline:'2020-08-20', content:'-', score:'100', actualScore:'-', comment:'-', action:21},
        {id:11, category:'C语言程序设计基础总结', requirement:'111111111', deadline:'2020-08-20', content:'-', score:'100', actualScore:'-', comment:'-', action:21},
        {id:12, category:'第1次周报', requirement:'11', deadline:'2020-07-11', content:'123', score:'25.00', actualScore:'20', comment:'1', action:21},
        {id:12, category:'第2次周报', requirement:'11', deadline:'2020-07-21', content:'-', score:'25.00', actualScore:'-', comment:'-', action:21},
        {id:12, category:'第3次周报', requirement:'11', deadline:'2020-07-31', content:'-', score:'25.00', actualScore:'-', comment:'-', action:21},
        {id:12, category:'第4次周报', requirement:'11', deadline:'2020-08-10', content:'-', score:'25.00', actualScore:'-', comment:'-', action:21}
      ],
      editDialogVisible: false,
      form: {},
      rules: {
        category: [
          { required: true, message: '请输入类别', trigger: 'blur' }
        ],
        requirement: [
          { required: true, message: '请输入填写要求', trigger: 'blur' }
        ],
        deadline: [
          { required: true, message: '请选择截至时间', trigger: 'change' }
        ],
        score: [
          { required: true, message: '请输入分数', trigger: 'blur' },
          { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的分数格式', trigger: 'blur' }
        ],
        actualScore: [
          { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的分数格式', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    filteredData() {
      return this.tableData.filter(item => {
        return (
          (this.filter.id === '' || item.id.toString().includes(this.filter.id)) &&
          (this.filter.category === '' || item.category.includes(this.filter.category))
        )
      })
    }
  },
  methods: {
    handleSearch() {
      this.$message({
        message: '搜索完成',
        type: 'success',
        duration: 1000
      })
    },
    resetSearch() {
      this.filter = {
        id: '',
        category: '',
        search: ''
      }
      this.$message({
        message: '已重置搜索条件',
        type: 'info',
        duration: 1000
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.editDialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确定要删除这条周报记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const index = this.tableData.findIndex(item => item.id === row.id && item.category === row.category)
        if (index !== -1) {
          this.tableData.splice(index, 1)
          this.$message.success('删除成功')
        }
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    handleDialogClosed() {
      this.$refs.form.resetFields()
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const index = this.tableData.findIndex(item => item.id === this.form.id && item.category === this.form.category)
          if (index !== -1) {
            this.tableData.splice(index, 1, { ...this.form })
            this.$message.success('修改成功')
            this.editDialogVisible = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.report-container {
  padding: 20px;
}

.filter-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.filter-item {
  margin-left: 10px;
}

.table-header th {
  background-color: #f5f7fa !important;
  font-weight: 600;
}

.pagination-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
  padding: 10px 20px;
  background: #f5f7fa;
}

.page-size {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.page-size span {
  margin-right: 5px;
  color: #606266;
}

.el-button [class*="el-icon-"] {
  margin-right: 5px;
}

.el-form-item {
  margin-bottom: 18px;
}

.el-date-editor, .el-input, .el-textarea {
  width: 100%;
}
</style>